#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
typedef pair<int, int> pii;
const int N = 1e5 + 10;
int dist[N];
bool st[N];
vector<pii> edges[N];
int n, m, s;
void kck()
{
    memset(dist, 0x3f, sizeof(dist));
    priority_queue<pii, vector<pii>, greater<pii>> q;
    q.push({ 0,s });
    dist[s] = 0;
    while (q.size())
    {
        auto t = q.top();
        q.pop();
        int u = t.second;
        if (st[u])
            continue;
        st[u] = true;
        for (auto& t : edges[u])
        {
            int v = t.first;
            int w = t.second;
            if (dist[u] + w < dist[v])
            {
                dist[v] = dist[u] + w;
                q.push({ dist[v],v });
            }
        }
    }
}
int main()
{
    cin >> n >> m >> s;
    for (int i = 1; i <= m; i++)
    {
        int u, v, w;
        cin >> u >> v >> w;
        edges[u].push_back({ v,w });
    }
    kck();
    for (int i = 1; i <= n; i++)
    {
        cout << dist[i] << " ";
    }
    return 0;
}